package com.youren.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.youren.health.constant.MessageConstant;
import com.youren.health.dao.CheckGroupDao;
import com.youren.health.entity.PageResult;
import com.youren.health.entity.QueryPageBean;
import com.youren.health.exception.HealthException;
import com.youren.health.pojo.CheckGroup;
import com.youren.health.pojo.CheckItem;
import com.youren.health.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: health_parent
 * @author: You_ren_a
 * @create: 2020-11-22 19:10
 **/
@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
//        if (!StringUtils.isEmpty(queryString)) {
//            queryString="%"+queryString+"%";
//        }
        Page<CheckGroup> page = checkGroupDao.findByPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional
    public void add(CheckGroup checkGroup, Integer[] checkItemIds) {
        checkGroupDao.add(checkGroup);
        Integer id = checkGroup.getId();
        if (checkItemIds!=null) {
            for (Integer checkItemId : checkItemIds) {
                checkGroupDao.addCheckGroupCheckItem(id,checkItemId);
            }
        }
    }

    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        return checkGroup;
    }

    @Override
    @Transactional
    public void update(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.update(checkGroup);
        Integer id = checkGroup.getId();
        checkGroupDao.deleteCheckGroupCheckItem(id);
        if (checkitemIds!=null) {
            for (Integer checkItemId : checkitemIds) {
                checkGroupDao.addCheckGroupCheckItem(id,checkItemId);
            }
        }
    }

    @Override
    public List<Integer> findCheckItemIdList(Integer id) {
        List<Integer> checkItemIdList = checkGroupDao.findCheckItemIdList(id);
        return checkItemIdList;
    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroupList=checkGroupDao.findAll();
        return checkGroupList;
    }



    @Override
    @Transactional
    public void deleteById(Integer id) throws HealthException{
        int count = checkGroupDao.findSetmealCountByCheckGroupId(id);
        if (count>0) {
            throw new HealthException(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        checkGroupDao.deleteCheckGroupCheckItem(id);
        checkGroupDao.deleteById(id);
    }
}